<?php

/**
 * CityBao (C)2010-2099 CityBao Inc.
 * This is NOT a freeware, use is subject to license terms
 *
 * Id: setting_site.php 2010-10-9 9:07:09
 */
if(!defined('CITY_BAO') || !defined('CB_ADMIN')) {
	exit('Access Denied');
}

if (!in_array($action, array('basic', 'register', 'credit', 'message', 'seo', 'server', 'upload', 'mail', 'safe', 'page-detail'))) {
    $action = 'basic';
}
$settings = $cbdb->get_results('SELECT * FROM {#cbdbPrefix}settings WHERE 1', ARRAY_A);
$tpl->assign('settings', normalizer_dbsettings($settings));
$form_validate = form_validate($action);
if ($request->isPost()) {
    $validate = trim($request->getParam('validate'));
    if ($validate == $form_validate) {
        $param = $request->getParam('param');
        foreach ($param as $key => $value) {
            $cbdb->update('{#cbdbPrefix}settings', array('value' => $value), array('key' => $key));
        }
        cb_cache_refresh('settings');

        show_message('新的设置已经保存成功！');
    } else {
        show_message('数据来源校验失败！', '-1');
    }
} else {
    $tpl->assign('title', '后台管理 -CityBao System');
    $tpl->assign('validate', $form_validate);
    if ('basic' === $action) {
        //读取模板
        $templates_option = array();
        $template_root = CB_ROOT . 'templates/';
        $dir_handle = dir($template_root);
        while (false !== ($entry = $dir_handle->read())) {
            if (!preg_match('/^\..*/', $entry)) {
                $entry1 = $template_root . $entry;
                if (is_dir($entry1)) {
                    $templates_option[$entry] = $entry;
                }
            }
        }
        $dir_handle->close();
        $tpl->assign('templates_option', $templates_option);
    }elseif('upload'=== $action){
        //读取字体
        $water_font_option = array();
        $font_root = CB_ROOT . 'data/fonts/';
        $dir_handle = dir($font_root);
        while (false !== ($entry = $dir_handle->read())) {
            if (preg_match('/(\.ttf)$/is', $entry)) {
                $entry1 = $font_root . $entry;
                if (is_file($entry1)) {
                    $water_font_option[$entry] = $entry;
                }
            }
        }
        $dir_handle->close();
        $tpl->assign('water_font_option', $water_font_option);
    }
    $tpl->display($module.'/setting_site/' . $action . '.tpl');
}